package com.aefottt.struggle_star.activity;

import android.animation.ValueAnimator;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;

import com.aefottt.struggle_star.R;
import com.aefottt.struggle_star.base.BaseActivity;
import com.aefottt.struggle_star.view.RippleView;

public class StarActivity extends BaseActivity {

    private LinearLayout llStar;
    private RippleView rippleView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_star;
    }

    @Override
    protected boolean isFullScreenMode() {
        return true;
    }

    @Override
    protected boolean isBlackFontStatusBar() {
        return false;
    }

    @Override
    protected void initView() {
        super.initView();
        llStar = findViewById(R.id.ll_star);
        rippleView = findViewById(R.id.ripple_view_star);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        setRippleAnim();
    }

    /**
     * 设置水波纹动画
     */
    private void setRippleAnim() {
        rippleView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                Intent intent = getIntent();
                int posX = intent.getIntExtra("posX", 0);
                int posY = intent.getIntExtra("posY", 0);
                Log.e(getClass().getName(), "posX:"+posX+" posY:"+posY);
                rippleView.getViewTreeObserver().removeOnPreDrawListener(this);
                rippleView.startRippleAnim(posX, posY);
                return true;
            }
        });
        rippleView.setOnRippleListener(state -> {
            if (state == RippleView.RIPPLE_START){
                llStar.setAlpha(0);
                rippleView.setVisibility(View.VISIBLE);
            }else {
                ValueAnimator animator = ValueAnimator.ofFloat(0, 1.0f);
                animator.setTarget(llStar);
                animator.setDuration(100);
                animator.start();
                rippleView.setVisibility(View.GONE);
            }
        });
    }
}